/**
 * Hook up the verifiable fields with the refered functions.
 */
registerJQueryFunction(function(selector){
		selector('[verify]').change(verifyElementReturnTrue);
		selector('[verify]').bind('autocompleteselect',verifyElementReturnTrue);
		selector('[verify]').bind('autocompletechange',verifyElementReturnTrue);
		selector('[verify]').each(verifyElementReturnTrue);
});

/**
 * Verify a single element. Return true if ok or false if there is error, set
 * the style to show if there was an error, and set the `inputErrorString` data
 * that says what the error is.
 */
function verifyElement(){
	var thisElement=$(this);
	if(1<thisElement.size()){
		var isThereError=false;
		thisElement.each(function(){
				if(!verifyElement.call(this)){
					isThereError=true;
				}
		});
		return isThereError;
	}
	var verifiers=getFunctions(thisElement.attr('verify'));
	for(var i in verifiers){
		var verifier=verifiers[i];
		if(!verifier.call(this,thisElement.val())){
			thisElement.addClass('input-error');
			thisElement.data('inputErrorString',verifier.errorString || verifier.name);
			return false;
		}
	}
	thisElement.removeClass('input-error');
	thisElement.removeData('inputErrorString');
	return true;
}

/**
 * Wrapper - verify(to set style and `inputErrorString` data) but always return true.
 */
function verifyElementReturnTrue(){
	verifyElement.call(this);
	return true;
}

/**
 * Verification function to verify the field has data.
 */
function notNull(data){
	return null!=data && ''!=data;
}
notNull.errorString='You have not entered a value.';

/**
 * Verification function to verify the date has not passed.
 */
function dateAlreadyPassed(data){
	return new Date().setHours(0,0,0)<=new Date(data).setHours(1,0,0);
}
dateAlreadyPassed.errorString='The date you entered has already passed.';

/**
 * Collect all input errors from a form.
 */
$.fn.collectAllErrors=function(){
	var errors={};
	$(this).find('.input-error').each(function(){
			errors[$(this).attr('name')]=$(this).data('inputErrorString') || 'error';
	});
	return errors;
};
